package com.minova.demo.controller;

import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.minova.demo.entity.TzCategory;
import com.minova.demo.entity.TzHotSearch;
import com.minova.demo.entity.TzNotice;
import com.minova.demo.entity.TzProd;
import com.minova.demo.entity.TzProdComm;
import com.minova.demo.service.ITzCategoryService;
import com.minova.demo.service.ITzHotSearchService;
import com.minova.demo.service.ITzNoticeService;
import com.minova.demo.service.ITzProdCommService;
import com.minova.demo.service.ITzProdService;
import com.minova.demo.service.impl.TzCategoryServiceImpl;
import com.minova.demo.service.impl.TzHotSearchServiceImpl;
import com.minova.demo.service.impl.TzProdServiceImpl;

@RestController()
@RequestMapping("TzCategory")
public class IndexController {
	
	@Autowired
	private ITzCategoryService tzCategoryServiceImpl;
	
	@Autowired
	private ITzProdService tzProdServiceImpl;
	
	@Autowired
	private ITzHotSearchService iTzHotSearchService;
	
	@Autowired
	private ITzProdCommService iTzProdCommService;
	
	@Autowired
	private ITzNoticeService iTzNoticeService;
	
	@RequestMapping("getCategory")
	@ResponseBody
	public List<TzCategory> getCategory() {
		return tzCategoryServiceImpl.selectAll();
	}
	
	@RequestMapping("getTzPord")
	@ResponseBody
	public List<TzProd> getTzPord(Integer cid){
		cid = cid==null?0:cid; 
		return tzProdServiceImpl.selectTzProdByCid(cid);
	}
	
	@RequestMapping("getHotSearch")
	@ResponseBody
	public List<TzHotSearch> getHotSearch(){
		return iTzHotSearchService.select();
	}

	@RequestMapping("test")
	@ResponseBody
	public String test(String content,Integer status,Integer istop) {
		content = content==null?"":content;
		status = status == null?3:status;
		istop = istop == null?3:istop;
		List<TzNotice> tlist =  iTzNoticeService.select(content, status, istop);
		StringBuffer sb = new StringBuffer("{\"code\":0,\"msg\":\"\",\"count\":"+tlist.size()+",\"data\":[");

		for (int i = 0; i < tlist.size(); i++) {

			String htmlStr = tlist.get(i).getContent();
			String textStr = "";
			try{
				String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
				String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
				String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
				
				Pattern p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
				Matcher m_script = p_script.matcher(htmlStr);
				htmlStr = m_script.replaceAll(""); //过滤script标签
				Pattern p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
				Matcher m_style = p_style.matcher(htmlStr);
				htmlStr = m_style.replaceAll(""); //过滤style标签
				Pattern p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
				Matcher m_html = p_html.matcher(htmlStr);
				htmlStr = m_html.replaceAll(""); //过滤html标签
				textStr = htmlStr;
				}catch(Exception e){
				//Manager.log.debug("neiNewsAction","Html2Text: " + e.getMessage());
				}
				//return textStr;//返回文本字符串
			
			textStr=textStr.replaceAll("&nbsp;", "");
			textStr=textStr.replaceAll("\r", "");
			textStr=textStr.replaceAll("\n", "");
			if(i==tlist.size()-1) {
				sb.append("{\"id\":"+tlist.get(i).getId()+",\"content\":\""+textStr+"\",\"status\":\""+tlist.get(i).getStatus()+"\",\"istop\":\""+tlist.get(i).getIsTop()+"\"}");
				break;
			}
			sb.append("{\"id\":"+tlist.get(i).getId()+",\"content\":\""+textStr+"\",\"status\":\""+tlist.get(i).getStatus()+"\",\"istop\":\""+tlist.get(i).getIsTop()+"\"},");
		}
		sb.append("]}");
		return sb.toString();
	}
	
	@RequestMapping("getSearchResult")
	@ResponseBody
	public List<TzProd> getSearchResult(String str,Integer descBy){
		descBy = descBy==null?0:descBy;
		return tzProdServiceImpl.getSearchResult(str,descBy);
	}
	
	@RequestMapping("getGoodRate")
	@ResponseBody
	public int getGoodRate(int prodid){ 
		return iTzProdCommService.getGoodRate(prodid);
	}

}
